package com.next

import static org.springframework.http.HttpStatus.*
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_CUSTOMER_SERVICE_REPRESENTATIVE', 'ROLE_ACCOUNT_MANAGER', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_GENERAL_RISK_MANAGER', 'ROLE_BRANCH_GENERAL_MANAGER', 'ROLE_BRANCH_OFFICE_RISK_MANAGER', 'ROLE_HEAD_OFFICE_RISK_MANAGER', 'ROLE_GENERAL_MANAGER', 'ROLE_CRO', 'ROLE_CEO', 'ROLE_POST_LOAN_MANAGER', 'ROLE_BRANCH_OFFICE_POST_LOAN_MANAGER'])
@Transactional(readOnly = true)
class AccountDebtController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond AccountDebt.list(params), model: [accountDebtCount: AccountDebt.count()]
    }

    def show(AccountDebt accountDebt)
    {
        respond accountDebt
    }

    def create()
    {
        def accountId = params.account
        def targetUri = request.getHeader("referer")
        respond new AccountDebt(params), model: [targetUri: targetUri, accountId: accountId]
    }

    @Transactional
    def save(AccountDebt accountDebt)
    {
        if (accountDebt == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountDebt.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountDebt.errors, model: [targetUri: params['targetUri'], accountId: accountDebt?.account?.id], view: 'create'
            return
        }

        accountDebt.save flush: true

        redirect controller: "account", action: "show1", method: "GET", id: accountDebt?.account?.id

    }

    def edit(AccountDebt accountDebt)
    {
        def targetUri = request.getHeader("referer")
        respond accountDebt, model: [targetUri: targetUri]
    }

    @Transactional
    def update(AccountDebt accountDebt)
    {
        if (accountDebt == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountDebt.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountDebt.errors, model: [targetUri: params['targetUri']], view: 'edit'
            return
        }

        accountDebt.save flush: true

        redirect controller: "account", action: "show1", method: "GET", id: accountDebt?.account?.id

    }

    @Transactional
    def delete(AccountDebt accountDebt)
    {

        if (accountDebt == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        accountDebt.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'accountDebt.label', default: 'AccountDebt'), accountDebt.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountDebt.label', default: 'AccountDebt'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
